package com.xinqi.modules.sys.role.controller.admin;

import com.xinqi.common.base.constants.ServiceNameConstants;
import com.xinqi.common.base.data.IdNameDTO;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.R;
import com.xinqi.common.log.syslog.annotation.Log;
import com.xinqi.common.log.syslog.enums.BusinessType;
import com.xinqi.common.log.syslog.enums.OperatorType;
import com.xinqi.modules.sys.role.dto.req.AddUserRoleDTO;
import com.xinqi.modules.sys.role.dto.req.RoleCreateDTO;
import com.xinqi.modules.sys.role.dto.req.RoleQueryDTO;
import com.xinqi.modules.sys.role.dto.req.RoleStatusUpdateDTO;
import com.xinqi.modules.sys.role.dto.req.RoleUpdateDTO;
import com.xinqi.modules.sys.role.dto.rsp.RoleResultDTO;
import com.xinqi.modules.sys.role.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统角色WEB控制层
 *
 * @author: linkai
 * @date: 2023/04/01
 */
@Tag(name = "Admin-系统角色", description = "系统角色Admin接口")
@RestController
@RequestMapping("/admin/role")
@RequiredArgsConstructor
public class RoleWebController {
    private final RoleService roleService;


    /**
     * 系统角色-分页
     */
    @Operation(summary = "系统角色-分页")
    @GetMapping("/page")
    public R<PageInfo<RoleResultDTO>> page(@ParameterObject RoleQueryDTO dto) {
        PageInfo<RoleResultDTO> page = roleService.page(dto, dto);
        return R.data(page);
    }

    /**
     * 系统角色-详情
     */
    @Operation(summary = "系统角色-详情")
    @GetMapping("/{id}")
    public R<RoleResultDTO> findById(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        RoleResultDTO dto = roleService.findById(id);
        return R.data(dto);
    }

    /**
     * 新增系统角色
     */
    @Operation(summary = "新增系统角色")
    @PostMapping
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 新增系统角色", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    public R<Long> insert(@RequestBody RoleCreateDTO dto) {
        return R.data(roleService.insert(dto));
    }

    /**
     * 修改系统角色
     */
    @Operation(summary = "修改系统角色")
    @PutMapping("/{id}")
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 修改系统角色", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    public R<Boolean> update(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id, @RequestBody RoleUpdateDTO dto) {
        dto.setId(id);
        return R.data(roleService.updateById(dto));
    }

    /**
     * 删除系统角色
     */
    @Operation(summary = "删除系统角色")
    @DeleteMapping("/{id}")
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 删除系统角色", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    public R<Boolean> delete(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        return R.data(roleService.deleteById(id));
    }

    /**
     * 新增用户角色
     *
     * @param record 新增实体
     * @return 新增结果
     */
    @Operation(summary = "新增用户角色")
    @PostMapping(value = "/add/user-role")
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 新增用户角色", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    public R<Boolean> addUserRole(@RequestBody AddUserRoleDTO record) {
        return R.data(roleService.addUserRole(record));
    }

    /**
     * 查询用户角色列表
     */
    @Operation(summary = "查询用户角色")
    @GetMapping(value = "/user-role")
    public R<List<RoleResultDTO>> findUserRole(@Parameter(description = "用户Id", example = "100001") Long userId) {
        return R.data(roleService.findUserRole(userId));
    }

    /**
     * 查询当前用户角色列表
     */
    @Operation(summary = "查询当前用户角色")
    @GetMapping(value = "/current-user-role")
    public R<List<RoleResultDTO>> findCurrentUserRole() {
        return R.data(roleService.findCurrentUserRole());
    }

    /**
     * 校验角色名称
     */
    @Operation(summary = "校验角色名称")
    @PostMapping(value = "/checkName")
    public R<Boolean> checkName(@RequestBody IdNameDTO checkName) {
        return R.data(roleService.isExistName(checkName.getName(), checkName.getId()));
    }

    @Operation(summary = "修改角色状态")
    @PutMapping(value = "/status")
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 修改角色状态", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    public R<Boolean> updateStatus(@RequestBody RoleStatusUpdateDTO dto) {
        return R.data(roleService.updateStatus(dto));
    }

}
